/**
 * PA控制口
 * <p>
 * 不同PA控制逻辑不一样，需硬件工程师供，参看SDK开发文档附录一表格
 */
package com.simdo.g768.Util;

import com.g768.Bean.JsonObject;
import com.g768.Bean.LteBand;
import com.g768.Bean.NrBand;
import com.g768.MessageControl.MessageController;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PaCtl {
    private static PaCtl instance;

    public static PaCtl build() {
        synchronized (PaCtl.class) {
            if (instance == null) {
                instance = new PaCtl();
            }
        }
        return instance;
    }

    public PaCtl() {
    }

    // 关闭所有小区的功放
    // 一般在业务启动前配置一次，避免两种可预见的两种情况影响：
    // 1、基带板上电时默认功放全是 1
    // 2、上一次配置的功放，结束时未关闭
    public void closePA(String id) {
        int[] arr = new int[]{1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 0};
        AppLog.D("closePA: " + Arrays.toString(arr));
        MessageController.build().setGpioPa(id, 0, arr);
    }

    public JsonObject getGpioJson(int arfcn, int cellId) {
        List<int[]> arrList = getGpioIntArrList(arfcn, cellId);
        int[] open_rx = arrList.get(0);
        int[] open_txrx = arrList.get(1);
        int[] close_txrx = arrList.get(2);

        JsonObject jsonValue = new JsonObject();

        JSONArray array = new JSONArray();
        for (int gpo : open_rx) array.put(gpo);
        jsonValue.put("open_rx", array);

        array = new JSONArray();
        for (int gpo : open_txrx) array.put(gpo);
        jsonValue.put("open_txrx", array);

        array = new JSONArray();
        for (int gpo : close_txrx) array.put(gpo);
        jsonValue.put("close_txrx", array);

        return jsonValue;
    }

    public List<int[]> getGpioIntArrList(int arfcn, int cellId) {
        int[] open_rx;
        int[] open_txrx;
        int[] close_txrx;

        String band = arfcn < 100000 ? "B" + LteBand.earfcn2band(arfcn) : "N" + NrBand.earfcn2band(arfcn);
        switch (band) {
            case "B3":
                open_rx = new int[]{0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "N79":
                open_rx = new int[]{1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = new int[]{4, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case "B40":
                open_rx = new int[]{0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = new int[]{0, 0, 0, 0, 4, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case "N78":
                open_rx = new int[]{0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = new int[]{0, 0, 0, 0, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case "B1":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "N28":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "B34":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case "B5":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "B28":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "B39":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case "B7":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "N1":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0};
                open_txrx = open_rx;
                break;
            case "B41":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0};
                open_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 2, 0, 0, 0, 0};
                break;
            case "B8":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0};
                open_txrx = open_rx;
                break;
            case "N41":
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0};
                open_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 0};
                break;
            default:
                open_rx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                open_txrx = open_rx;
        }

        switch (cellId) {
            case 0:
                close_txrx = new int[]{1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case 1:
                close_txrx = new int[]{0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case 2:
                close_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case 3:
                close_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
            case 4:
                close_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0};
                break;
            case 5:
                close_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0};
                break;
            default:
                close_txrx = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                break;
        }

        List<int[]> list = new ArrayList<>();
        list.add(open_rx);
        list.add(open_txrx);
        list.add(close_txrx);

        return list;
    }
}
